/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[Person.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package swing.listener;

/**
 * 事件处理模型涉及到三个组件: 事件源[Person], 事件对象[PersonEvent], 事件监听器[PersonListener]
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class Person {

	private String name;
	private PersonListener listener;

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public Person(String name) {
		this.name = name;
	}

	public void registerListener(PersonListener listener) {
		this.listener = listener;
	}

	public void eat() {
		if (listener != null) {
			// 调用监听器对象的doEat方法监听Person对象的eat动作, 将事件对象PersonEvent传递给doEat方法,
			// 事件对象封装了事件源, new PersonEvent(this)中的this代表的就是事件源
			listener.doEat(new PersonEvent(this));
		}
	}

	public void run() {
		if (listener != null) {
			listener.doRun(new PersonEvent(this));
		}
	}

	@Override
	public String toString() {
		return this.name;
	}
}
